package controllers;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;

import workerwithdb.managers.ServerHandler;
import api.controller.IAutoservice;
import api.messages.Messager;

public class Autoservice implements IAutoservice {

	private ServerHandler serverHandler;
	private ArrayList<String> exceptionMap = new ArrayList<String>();

	public Autoservice() {
		serverHandler = new ServerHandler();
	}
	

	public ArrayList<String> getEmptyGarage() {
		return serverHandler.getEmptyPlaces();
	}

	public ArrayList<String> SortOrderByBeginningDate() {
		return serverHandler.sortOrderByBeginningDate();
	}

	public ArrayList<String> SortOrderByDeadlineDate() {
		return serverHandler.sortOrderByDeadlineDate();
	}

	public ArrayList<String> SortOrderByFillingDate() {
		return serverHandler.sortOrderByFillingDate();
	}

	public ArrayList<String> SortOrderByPrice() {
		return serverHandler.sortOrderByPrice();
	}

	public ArrayList<String> SortMasterByEmployment() {
		return serverHandler.sortMasterByEmployment();
	}

	public ArrayList<String> SortMasterByName() {
		return serverHandler.sortMasterByName();
	}

	public ArrayList<String> SortMadingOrderByBeginningDate() {
		return serverHandler.sortMadingOrderByBeginningDate();

	}

	public ArrayList<String> SortMadingOrderByDeadlineDate() {
		return serverHandler.sortMadingOrderByDeadlineDate();
	}

	public ArrayList<String> SortMadingOrderByFillinggDate() {
		return serverHandler.sortMadingOrderByFillinggDate();

	}

	public ArrayList<String> SortMadingOrderByPrice() {
		return serverHandler.sortMadingOrderByPrice();

	}

	public ArrayList<String> getOrderByMaster(String parameters) {
		return serverHandler.getOrderByMaster(parameters);
	}

	public ArrayList<String> getMasterByOrder(String parameters) {
		Integer numberOrder = Integer.parseInt(parameters);
		return serverHandler.getMasterByOrder(numberOrder);
	}

	public ArrayList<String> SortDeletedOrderByPeriodByBeginningDate(
			String parameters) {
	
		try {
			ClassParserObject.setArray(parameters);
			Date begin = ClassParserObject.getDate(0);
			Date end = ClassParserObject.getDate(1);
			return serverHandler
					.sortDeletedOrderByPeriodByBeginningDate(begin, end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortCanceledOrderByPeriodByBeginningDate(
			String parameters) {
		try {
			ClassParserObject.setArray(parameters);
			Date begin = ClassParserObject.getDate(0);
			Date end = ClassParserObject.getDate(1);
			return serverHandler.sortCanceledOrderByPeriodByBeginningDate(begin,
					end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortMadeOrderByPeriodByBeginningDate(
			String parameters) {
		try {
			ClassParserObject.setArray(parameters);
			Date begin = ClassParserObject.getDate(0);
			Date end = ClassParserObject.getDate(1);
			return serverHandler.sortMadeOrderByPeriodByBeginningDate(begin, end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortDeletedOrderByPeriodByDeadlineDate(
			String parameters) {
		try {
			ClassParserObject.setArray(parameters);
			Date begin = ClassParserObject.getDate(0);
			Date end = ClassParserObject.getDate(1);
			return serverHandler.sortDeletedOrderByPeriodByDeadlineDate(begin, end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortCanceledOrderByPeriodByDeadlineDate(
			String parameters) {
		try {
			ClassParserObject.setArray(parameters);
			Date begin = ClassParserObject.getDate(0);
			Date end = ClassParserObject.getDate(1);
			return serverHandler
					.sortCanceledOrderByPeriodByDeadlineDate(begin, end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortMadeOrderByPeriodByDeadlineDate(
			String parameters) {
		try {
			ClassParserObject.setArray(parameters);
			Date begin = ClassParserObject.getDate(0);
			Date end = ClassParserObject.getDate(1);
			return serverHandler.sortMadeOrderByPeriodByDeadlineDate(begin, end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortDeletedOrderByPeriodByFillingDate(
			String parameters) {
		try {ClassParserObject.setArray(parameters);
			Date begin = ClassParserObject.getDate(0);
			Date end = ClassParserObject.getDate(1);
			return serverHandler.sortDeletedOrderByPeriodByFillingDate(begin, end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortCanceledOrderByPeriodByFillingDate(
			String parameters) {
		try {
			ClassParserObject.setArray(parameters);
			Date begin = ClassParserObject.getDate(0);
			Date end = ClassParserObject.getDate(1);
			return serverHandler.sortCanceledOrderByPeriodByFillingDate(begin, end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortMadeOrderByPeriodByFillingDate(
			String parameters) {
		try {
			ClassParserObject.setArray(parameters);
			Date begin = ClassParserObject.getDate(0);
			Date end = ClassParserObject.getDate(1);
			return serverHandler.sortMadeOrderByPeriodByFillingDate(begin, end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortDeletedOrderByPeriodByPrice(String parameters) {
		try {
			ClassParserObject.setArray(parameters);
			Date begin = ClassParserObject.getDate(0);
			Date end = ClassParserObject.getDate(1);
			return serverHandler.sortDeletedOrderByPeriodByPrice(begin, end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}

	}

	public ArrayList<String> SortCanceledOrderByPeriodByPrice(String parameters) {
		try {
			ClassParserObject.setArray(parameters);
			Date begin = ClassParserObject.getDate(0);
			Date end = ClassParserObject.getDate(1);
			return serverHandler.sortCanceledOrderByPeriodByPrice(begin, end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}

	}

	public ArrayList<String> SortMadeOrderByPeriodByPrice(String parameters) {
		try {
			ClassParserObject.setArray(parameters);
			Date begin = ClassParserObject.getDate(0);
			Date end = ClassParserObject.getDate(1);
			return serverHandler.sortMadeOrderByPeriodByPrice(begin, end);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}

	}

	public ArrayList<String> getFreePlacesOnDate(String parameters) {
		try {
			ClassParserObject.setArray(parameters);
			return serverHandler.getEmptyPlacesOnDate(ClassParserObject.getDate(0));
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> getNextFreeDate() {
		return serverHandler.getNextFreeDate();
	}

	public ArrayList<String> addMaster(String parameters) {
		return serverHandler.addMaster(parameters);
	}

	public ArrayList<String> deleteMaster(String parameters) {
		return serverHandler.deleteMaster(parameters);
	}

	public ArrayList<String> addGarage(String parameters) {
		ClassParserObject.setArray(parameters);
		return serverHandler.addGarage(ClassParserObject.getInt(0));
	}

	public ArrayList<String> deleteGarage(String parameters) {
		ClassParserObject.setArray(parameters);
		return serverHandler.deleteGarage( ClassParserObject.getInt(0));
	}

	public ArrayList<String> addOrder(String parameters) {
		ClassParserObject.setArray(parameters);
		Integer orderNumber = ClassParserObject.getInt(0);
		try {
			Date fillingDate = ClassParserObject.getDate(1);
			Date deadlineDate = ClassParserObject.getDate(2);
			Date beginingDate = ClassParserObject.getDate(3);
			Integer price = ClassParserObject.getInt(4);
			String masterName = ClassParserObject.getString(5);
			Integer numberGarage = ClassParserObject.getInt(6);

			return serverHandler.addOrder(orderNumber, fillingDate, deadlineDate,
					beginingDate, price, masterName, numberGarage);

		} catch (ParseException e) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;

		}
	}

	public ArrayList<String> markOrderDeleted(String parameters) {
		ClassParserObject.setArray(parameters);
		return serverHandler.markOrderDeleted(ClassParserObject.getInt(0));
	}

	public ArrayList<String> markOrderCanceled(String parameters) {
		ClassParserObject.setArray(parameters);
		return serverHandler.markOrderCanceled(ClassParserObject.getInt(0));
	}

	public ArrayList<String> markOrderMade(String parameters) {
		ClassParserObject.setArray(parameters);
		return serverHandler.markOrderMade(ClassParserObject.getInt(0));
	}

	public ArrayList<String> ShiftTimeOrder(String parameters) {
		ClassParserObject.setArray(parameters);
		try {
			Date newDate =ClassParserObject.getDate(0);
			Integer numberOrder = ClassParserObject.getInt(1);
			return serverHandler.shiftOrdersDeadlineDate(newDate, numberOrder);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> cloneOrder(String parameters) {
		ClassParserObject.setArray(parameters);
		return serverHandler.cloneOrder(ClassParserObject.getInt(0));
	}

	public ArrayList<String> exportGarages() {
		return serverHandler.exportGarages();

	}

	public ArrayList<String> exportMasters() {
		return serverHandler.exportMasters();

	}

	public ArrayList<String> exportOrders() {
		return serverHandler.exportOrders();
	}

	public ArrayList<String> importGarages() {
		return serverHandler.importGarages();
	}

	public ArrayList<String> importMasters() {
		return serverHandler.importMasters();
	}

	public ArrayList<String> importOrders() {
		return serverHandler.importOrders();
	}

}
